const util = require('../../../../../../utils/util.js');
const api = require('../../../../../../config/api.js');
import Dialog from '../../../../../../ucenter/lib/vant/dialog/dialog';
const app = getApp();
Page({
  data: {
    logininfo: {},
    intro: '',
    srwx:'',
    introLength: 0,
    //行数
    line: '',
    //字符数量
    maxzf: 100,
  },
  onLoad: function(){
    this.setData({
      logininfo: app.globalData.logininfo,
    })
  },
  onReady: function(){
    var logininfo = this.data.logininfo
    this.setData({
      intro: logininfo.homeInfo.circleIntroduction,
      srwx: logininfo.homeInfo.wechat,
      introLength: logininfo.homeInfo.circleIntroduction.length
    })
  },
  //介绍输入文本
  okintro: function(e){
    var _this = this 
    var intro = _this.data.intro
    var srwx = _this.data.srwx
    var openid = app.globalData.logininfo.openid;
    var data = {};
    if (openid != null && openid != "") {
      util.post(api.fetchToken()).then(function (res) {
        if (res.access_token != '') {
          return Promise.resolve({
            access_token: res.access_token
          });
        }
      }).then(function (res) {
        data.openid = openid;
        data.access_token = res.access_token;
        data.circleIntroduction = intro;
        data.wechat = srwx;
        util.post(api.My_doUpdateHomeInfo, data).then(function (res) {
          if (res.code == 200) {
            wx.showToast({
              title: '保存中',
              icon: 'loading',
              duration: 500
            })
            console.log(res)
          }else{
            wx.showToast({
              title: '保存失败',
              icon: 'none',
              duration: 1000
            })
          }
        });
      });
    }
    var logininfo = app.globalData.logininfo;
    logininfo.homeInfo.circleIntroduction = intro;
    wx.setStorage({
      key: 'logininfo',
      data: logininfo
    })
  },
  //介绍文本输入的字符数
  jszf: function(e){
    var _this = this
    var dValue = e.detail.value.replace(/[\r\n]/g, '')
    var dLength = dValue.length
    _this.setData({
      intro: dValue,
      introLength: dLength
    })
  },
  introline: function(e){
  },
  //微信输入文本
  okwx: function (e) {
    var dValue = e.detail.value.replace(/\s+/g, '')
    var _this = this
    _this.setData({
      srwx: dValue
    })
    var intro = _this.data.intro
    var srwx = _this.data.srwx
    var openid = app.globalData.logininfo.openid;
    var data = {};
    if (openid != null && openid != "") {
      util.post(api.fetchToken()).then(function (res) {
        if (res.access_token != '') {
          return Promise.resolve({
            access_token: res.access_token
          });
        }
      }).then(function (res) {
        data.openid = openid;
        data.access_token = res.access_token;
        data.circleIntroduction = intro;
        data.wechat = srwx;
        util.post(api.My_doUpdateHomeInfo, data).then(function (res) {
          if (res.code == 200) {
            wx.showToast({
              title: '保存中',
              icon: 'loading',
              duration: 500
            })
            console.log(res)
          } else {
            wx.showToast({
              title: '保存失败',
              icon: 'none',
              duration: 1000
            })
          }
        });
      });
    }
    var logininfo = app.globalData.logininfo;
    logininfo.homeInfo.wechat = srwx;
    wx.setStorage({
      key: 'logininfo',
      data: logininfo
    })
  }
})